//
//  HeroInformationViewController.m
//  LOLProject
//
//  Created by lanou3g on 15/6/4.
//  Copyright (c) 2015年 lanou3g.com蓝欧科技. All rights reserved.
//

#import "HeroInformationViewController.h"

#define ITEM_SPACING 200

@interface HeroInformationViewController ()

@end

@implementation HeroInformationViewController

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder]))
    {
        self.wrap = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    设置背景颜色
    self.view.backgroundColor=[UIColor whiteColor];
    self.carousel=[[iCarousel alloc] initWithFrame:CGRectMake(0, 25+44, CGRectGetWidth([[UIScreen mainScreen] bounds]), CGRectGetHeight([[UIScreen mainScreen] bounds])-25-44-44)];
    [self.view addSubview:self.carousel];
    self.carousel.delegate = self;
    self.carousel.dataSource = self;
    self.carousel.type = iCarouselTypeCoverFlow;
    self.navItem.title = @"英雄介绍";
//    给所有英雄数组分配空间,并赋初值
    self.allHerosDataArray=[NSMutableArray array];
//    从网上请求数据
    [self requestAllHerosData];
}

                      
#pragma mark---从网上请求数据
- (void) requestAllHerosData{
//    同步连接下的GET请求
//    1.创建网址对象
    NSURL * url=[NSURL URLWithString:kHeroVideoData_Collection_URL];
//    2.创建网络请求对象
    NSURLRequest * request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
//    3.创建连接对象,发送请求
    NSURLResponse * response=nil;
    NSError * error=nil;
    NSData * data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//    4.解析数据
//        代码保护
    if (data==nil) {
        return ;
    }
//        4.解析数据
    NSDictionary * sourceDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        处理数据(封装model对象)
    for (NSDictionary * dic in [sourceDic objectForKey:@"all"]) {
        Hero * hero=[[Hero alloc] init];
        [hero setValuesForKeysWithDictionary:dic];
        [self.allHerosDataArray addObject:hero];
    }
//    更新视图
    [self.carousel reloadData];
    return ;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.carousel = nil;
    self.navItem = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (IBAction)switchCarouselType
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择类型" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"直线", @"圆圈", @"反向圆圈", @"圆桶", @"反向圆桶", @"封面展示", @"封面展示2", @"纸牌", nil];
    [sheet showInView:self.view];
}

- (IBAction)toggleWrap
{
    self.wrap = !self.wrap;
    self.navItem.rightBarButtonItem.title = self.wrap? @"边界:ON": @"边界:OFF";
    [self.carousel reloadData];
}

#pragma mark -

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    for (UIView *view in self.carousel.visibleItemViews)
    {
        view.alpha = 1.0;
    }
    
    [UIView beginAnimations:nil context:nil];
    self.carousel.type = (int)buttonIndex;
    [UIView commitAnimations];
    
//    self.navItem.title = [actionSheet buttonTitleAtIndex:buttonIndex];
}

#pragma mark -

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [self.allHerosDataArray count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
//    创建button,并使用自定义类型
    UIButton * button=[UIButton buttonWithType:UIButtonTypeCustom];
//    设置button的frame
    button.frame=CGRectMake(70, 80, 180, 260);
    [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[[self.allHerosDataArray objectAtIndex:index] enName]]] forState:UIControlStateNormal];
//    设置button的title
    [button setTitle:[[self.allHerosDataArray objectAtIndex:index] title] forState:UIControlStateNormal];
//    设置title的字体大小
    button.titleLabel.font=[UIFont systemFontOfSize:18];
//    设置title的字体居中
    button.titleLabel.textAlignment=NSTextAlignmentCenter;
//    设置title在button上的位置
    button.titleEdgeInsets=UIEdgeInsetsMake(220, -button.titleLabel.bounds.size.width-60, 0, 0);
//    设置title在一般情况下为白色字体 
    [button setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
//    给button添加方法
    [button addTarget:self action:@selector(goHerosInFormationDetail:) forControlEvents:UIControlEventTouchUpInside];
//    设置button的tag值,为了以后点击button时用
    button.tag=1000+index;
    return button;
}

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
    return 0;
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    return [self.allHerosDataArray count];
}

- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    return ITEM_SPACING;
}

- (CATransform3D)carousel:(iCarousel *)_carousel transformForItemView:(UIView *)view withOffset:(CGFloat)offset
{
    view.alpha = 1.0 - fminf(fmaxf(offset, 0.0), 1.0);
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = self.carousel.perspective;
    transform = CATransform3DRotate(transform, M_PI / 8.0, 0, 1.0, 0);
    return CATransform3DTranslate(transform, 0.0, 0.0, offset * self.carousel.itemWidth);
}

- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
    return self.wrap;
}

#pragma mark---实现给button添加的方法
- (void) goHerosInFormationDetail:(UIButton *) button{
//    创建英雄信息详情界面视图控制器
    HeroInformationDetailViewController * heroInformationDetailVC=[[HeroInformationDetailViewController alloc] initWithNibName:@"HeroInformationDetailViewController" bundle:[NSBundle mainBundle]];
//    将英雄信息详情界面控制器作为heroInformationDetailNC的根视图控制器
    UINavigationController * heroInformationDetailNC=[[UINavigationController alloc] initWithRootViewController:heroInformationDetailVC];
//    获取对应的hero对象
    Hero * hero=[self.allHerosDataArray objectAtIndex:button.tag %1000];
//    设置英雄信息详情界面的标题
    heroInformationDetailVC.navigationItem.title=hero.enName;
//    传值
    heroInformationDetailVC.hero=hero;
//    模态推出英雄详情界面
    [self presentViewController:heroInformationDetailNC animated:YES completion:nil];
    return ;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
